import React, { useState } from 'react';
import { Form, Input, Modal, Radio } from 'antd';


const FormItem = Form.Item;

interface CreateFormProps {
  modalVisible: boolean;
  onSubmit: (values: API.RoleItem) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};

const CreateForm: React.FC<CreateFormProps> = (props) => {
  const [formVals] = useState<API.RoleItem>({
    roleName: '',
    roleDescription: '',
    roleLevel: 0,
    roleIsdisable: 0,
  });

  const [form] = Form.useForm();

  const { modalVisible, onSubmit: handleAdd, onCancel } = props;
  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
    form.resetFields();
    handleAdd(fieldsValue);
  };
  return (
    <Modal
      width={640}
      destroyOnClose
      title="新增角色"
      visible={modalVisible}
      onOk={okHandle}
      onCancel={() => onCancel()}
    >
      <Form {...formLayout}
        form={form}
        initialValues={{
          roleName: formVals.roleName,
          roleDescription: formVals.roleDescription,
          roleLevel: formVals.roleLevel,
          roleIsdisable: formVals.roleIsdisable,
        }}>
        <FormItem
          label="名称"
          name="roleName"
          rules={[{ required: true, message: '请输入至少二个字符！', min: 2 }]}
        >
          <Input placeholder="请输入" autoComplete='off' />
        </FormItem>
        <FormItem
          label="描述"
          name="roleDescription"
        >
          <Input.TextArea rows={4} placeholder="请输入" />
        </FormItem>
        <FormItem
          label="状态"
          name="roleIsdisable"
        >
          <Radio.Group>
            <Radio value={0} >
              启用
            </Radio>
            <Radio value={1}>
              禁用
            </Radio>
          </Radio.Group>
        </FormItem>
      </Form>
    </Modal>
  );
};

export default CreateForm;
